package com.etime.perfectschool.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.perfectschool.pojo.SchoolFood;
import com.etime.perfectschool.service.SchoolFoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@CrossOrigin
public class SchoolFoodController {

    @Autowired
    private SchoolFoodService schoolFoodService;


    //查询全部美食
    @GetMapping("/getAllSchoolFood")
    public Page<SchoolFood> getAllSchoolFood(int current, int size) {
        return schoolFoodService.getAllSchoolFood(current,size);
    }
    //发布美食
    @PostMapping("/addSchoolFood")
    public int addSchoolFood(SchoolFood schoolFood)
    {
        return schoolFoodService.addSchoolFood(schoolFood);
    }

    //删除美食
    @DeleteMapping("/deleteSchoolFood")
    public boolean deleteSchoolFood(int foodId)
    {
        return schoolFoodService.deleteSchoolFood(foodId);
    }
    //点赞功能的实现
    @PutMapping("/likeSchoolFood")
    public SchoolFood likeSchoolFood(int foodId)
    {
        int count=0;
        SchoolFood schoolFood = schoolFoodService.getSchoolFood(foodId);
        count=schoolFood.getFoodUpvote();
        if(count==0)
        {
            schoolFood.setFoodUpvote(1);
        }else {
            count=count+1;
            schoolFood.setFoodUpvote(count);
        }
        boolean res = schoolFoodService.likeSchoolFood(schoolFood);
        if(res)
        {
            schoolFood = schoolFoodService.getSchoolFood(foodId);
        }
        return schoolFood;
    }
    //查询某条美食
    @GetMapping("/getSchoolFood")
    public SchoolFood getSchoolFood(int foodId)
    {
        return schoolFoodService.getSchoolFood(foodId);
    }

    //修改美食
    @PutMapping("/updateSchoolFood")
    public boolean updateSchoolFood(SchoolFood schoolFood)
    {
        return schoolFoodService.updateSchoolFood(schoolFood);
    }

}
